
import { labelCreate ,labelDelete,labelUpdate } from './labelListServ'

import { parse } from 'qs'

import { message } from 'antd'

import moment from 'moment'

import {isEmpty, isArray, cloneDeep, merge} from 'lodash'

import { hashHistory } from 'dva/router'

const State = {
    biz: {
        queryForm: {}
    },
  showTabelModal:false,
  actionType:'add',
  modalTitle:'',
  detailData:{
      labelList:[],
      type:'',
      name:"",
      tagId:""
  }
}

// 声明module
const tmpModule = {
  state: cloneDeep(State),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          // dispatch({type: 'queryInfo', payload: {}}) // end dispatch
        }
      })
    },
  },

  // 方法列表
  effects: {

      *setFormVal({ payload }, { put, call, select }) {
          let { biz }  = yield select(d => d[tmpModule.namespace])
          try{
              yield put({type: 'updateStore', payload: {biz: payload}})
          }catch(e){
              console.error(e)
          }
      },
      // 删除标签
      *labelDel({ payload }, { put, call, select }) {
          let {biz} = yield select(d => d[tmpModule.namespace])
          try{
            const { data , success } = yield call(labelDelete, parse({ tagId:payload.tagId }))
              if(success){
                biz.refreshListDatas(true)

              }
          }catch(e){
              console.error(e)
          }
      },
      // 新增标签
      *labelAdd({ payload }, { put, call, select }) {
          let {biz,detailData} = yield select(d => d[tmpModule.namespace])
          try{
              const { data , success,pageNo } = yield call(labelCreate, parse({
                  name:detailData.labelList,
                  type:detailData.type
              }))
              if(success){
                  biz.refreshListDatas()
                  yield put({type: 'updateStore', payload: { showTabelModal: false,
                          detailData:{
                              labelList:[],
                              type:'',
                              name:"",
                              tagId:""
                          }}})
              }
          }catch(e){
              console.error(e)
          }
      },
      // 标签标签
      *update({ payload }, { put, call, select }) {
          let {biz,detailData} = yield select(d => d[tmpModule.namespace])
          try{
              const { data , success } = yield call(labelUpdate, parse({
                  name:detailData.name,
                  type:detailData.type,
                  tagId:detailData.tagId,
              }))
              if(success){
                  biz.refreshListDatas(true)
                  yield put({type: 'updateStore', payload: { showTabelModal: false}})
              }
          }catch(e){
              console.error(e)
          }
      },



    *updateModel({ payload }, { put,select}) {
        yield put({type: 'updateStore', payload });
    },
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
